package com.ssb.plugins;

import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.ssb.util.DateCalculationUtil;

import java.util.*;

/**
 * @Classname SelectOwnerPromotionFeePluginImpl
 * @Description 查询我的推广费列表
 * @Date 2020/4/15 18:42
 * @Created by fpc
 */
public class SelectOwnerPromotionFeePluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String userId = String.valueOf(map.get("ssCommissionDetail.w_userId"));
        String groupType = String.valueOf(map.get("ssCommissionDetail.w_dateType"));
        String formatGroup = "%Y-%m";
        if("year".equals(groupType)){
            formatGroup = "%Y";
        }
        String startDate = StrUtil.doNull(String.valueOf(map.get("startDate")),"2020-02");
        String endDate = StrUtil.doNull(String.valueOf(map.get("startDate")), DateUtils.Now.fmt_yyyyMM());
        List<String> monthList = DateCalculationUtil.getMonthBetween(startDate, endDate);
        String cgsql = getCommissionGroupSql(userId,formatGroup,startDate,endDate);
        List<DataMap> cgList = (List<DataMap>)IusPluginUtil.getInstance().select("ssb", cgsql, 0, 0);
        String casql = getCommissionApplySql(userId,formatGroup,startDate,endDate);
        List<DataMap> caList = (List<DataMap>)IusPluginUtil.getInstance().select("ssb", casql, 0, 0);
        Map<String,String> caMap = new HashMap();
        for (DataMap dataMap:caList){
            caMap.put(String.valueOf(dataMap.get("yearmonth")),String.valueOf(dataMap.get("money")));
        }
        DataMap tempMap =null;
        String applyMoney=null;
        for (String month:monthList) {
            boolean flag = true;
            applyMoney = StrUtil.doNull(caMap.get(month),"0.00");
            group:
            for (DataMap data : cgList) {
                if (data.get("yearmonth").equals(month)) {
                    flag = false;
                    data.put("applymoney",applyMoney);
                    break group;
                }
            }
            if(flag){
                tempMap = new DataMap();
                tempMap.put("yearmonth",month);//结算周期
                tempMap.put("num","0");//保单量
                tempMap.put("amount","0.00");//应得推广费
                tempMap.put("premium","0.00");//保费
                tempMap.put("applymoney",applyMoney);//已提现推广费
                cgList.add(tempMap);
            }
        }
        Collections.sort(cgList, new Comparator<DataMap>() {
            @Override
            public int compare(DataMap o1, DataMap o2) {
                String yearmonth1 =  String.valueOf(o1.get("yearmonth"));
                String yearmonth2 =  String.valueOf(o2.get("yearmonth"));
                return yearmonth2.compareTo(yearmonth1);
            }
        });
        Map returnMap = new HashMap();
        returnMap.put(CustomMethodPlugin.pluginInnerReturn, cgList);
        returnMap.put(CustomMethodPlugin.resultMark, "T");
        return returnMap;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
    private String getCommissionGroupSql(String userId,String formatGroup,String startDate,String endDate){
        String column = " DATE_FORMAT(scd.create_time, '"+formatGroup+"') AS yearmonth,COUNT(scd.info_id) AS num, SUM(scd.amount) AS amount, SUM(scd.premium) AS premium ";
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ");
        sb.append(column);
        sb.append("FROM ss_commission_detail scd WHERE ");
        sb.append(" scd.user_id ='"+userId+"'");
        sb.append(" AND DATE_FORMAT(scd.create_time, '%Y-%m') >='"+startDate+"'");
        sb.append(" AND DATE_FORMAT(scd.create_time, '%Y-%m') <='"+endDate+"'");
        sb.append("GROUP BY yearmonth");
        return sb.toString();
    }
    private String getCommissionApplySql(String userId,String formatGroup,String startDate,String endDate){
        String column = " DATE_FORMAT(sca.modify_time, '"+formatGroup+"') AS yearmonth, SUM(sca.money) AS money ";
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ");
        sb.append(column);
        sb.append("FROM ss_cash_apply sca WHERE sca.apply_status='90' ");
        sb.append(" AND sca.apply_user_id ='"+userId+"'");
        sb.append(" AND DATE_FORMAT(sca.modify_time, '%Y-%m') >='"+startDate+"'");
        sb.append(" AND DATE_FORMAT(sca.modify_time, '%Y-%m') <='"+endDate+"'");
        sb.append("GROUP BY yearmonth");
        return sb.toString();
    }
}
